﻿using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreCms.Net.Configuration;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.Expression;
using CoreCms.Net.Model.FromBody;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Filter;
using CoreCms.Net.Loging;
using CoreCms.Net.IServices;
using CoreCms.Net.Utility.Helper;
using CoreCms.Net.Utility.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using NPOI.HSSF.UserModel;
using SqlSugar;
using CoreCms.Net.Auth.HttpContextUser;
using CoreCms.Net.Repository;
using CoreCms.Net.Services;
using System.Collections.Generic;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace CoreCms.Net.Web.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HotbillboardController : ControllerBase
    {
        private readonly IHttpContextUser _user;
        private readonly IWebHostEnvironment _webHostEnvironment;
        private readonly IHotbillBoardServices _hotbillBoardServices;
        private readonly IHotbillboardThridServices _hotbillboardThridServices;
        private readonly IHotbillboardThridCategoryServices _hotbillboardThridCategoryServices;


     public HotbillboardController(IHttpContextUser user, IWebHostEnvironment webHostEnvironment
                , IHotbillBoardServices hotbillBoardServices
            , IHotbillboardThridServices hotbillboardThridServices
         , IHotbillboardThridCategoryServices hotbillboardThridCategoryServices

                )
        {
            _user = user;
            _webHostEnvironment = webHostEnvironment;
            _hotbillBoardServices = hotbillBoardServices;
            _hotbillboardThridServices = hotbillboardThridServices;
            _hotbillboardThridCategoryServices = hotbillboardThridCategoryServices;
        }
        #region 首页数据============================================================
        // POST: Api/coreshopordermeeting/GetIndex
        /// <summary>
        /// 首页数据,返回一场热门订货会
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Description("首页数据")]
        public async Task<WebApiCallBack> GetIndex()
        {
            var jm = new WebApiCallBack();

            Expression<Func<HotbillBoard, object>> orderEx = "id" switch
            {
                "id" => p => p.id,

                _ => p => p.id
            };

            var orderBy = "asc" switch
            {
                "asc" => OrderByType.Asc,
                "desc" => OrderByType.Desc,
                _ => OrderByType.Desc
            };

            //获取数据
            var list = await _hotbillBoardServices.QueryPageAsync(m => m.isShow == true, orderEx, orderBy, 0, 10, true);
            if (list.Count == 0)
            {
                jm.code = 400;
                jm.msg = "查询失败";
                return jm;
            }
        


            //返回数据
            jm.data = list;
            jm.code = 0;
            jm.status = true;
            jm.msg = "数据调用成功!";
            return jm;

        }


        [HttpGet]
        public async Task<WebApiCallBack> GetBoard([FromQuery] int boardid)
        {
            var jm = new WebApiCallBack();

        var list=   _hotbillBoardServices.GetDb().Queryable<HotbillboardThrid>().Where(m => m.boardId == boardid).ToList();

            jm.data = list;
            jm.status = true;
            jm.code = 0;
            jm.msg = "数据调用成功!";
            return jm;
        }


        [HttpGet]

        public async Task<WebApiCallBack> GetThirdCategory()
        {
            var jm = new WebApiCallBack();
            var list = _hotbillboardThridCategoryServices.QueryListByClause(m => m.isShow == true);

            jm.data= list;
            jm.status = true;
            jm.code = 0;
            jm.msg = "数据调用成功!";
            return jm;
        }

        [HttpGet]

        public async Task<WebApiCallBack> GetCategory(int thirdCategoryId)
        {
            var jm = new WebApiCallBack();

            var list = _hotbillBoardServices.GetDb().Queryable<CoreCmsGoodsCategory, HotbillBoard>
                ((c, h) => c.id == h.categoryId)
                .Where((c, h) => h.thridCategoryId == thirdCategoryId).Distinct().Select((c, h) => new 
                {
                    name = c.name,
                    id = c.id,
                    imageUrl = c.imageUrl,

                    sort = c.sort,


                }).ToList();


            jm.data = list;
            jm.status = true;
            jm.code = 0;
            jm.msg = "数据调用成功!";
            return jm;
        }


        public async Task<WebApiCallBack> GetList(int categoryId,int thirdCategoryId)
        {
            var jm = new WebApiCallBack();

            var list = _hotbillBoardServices.QueryListByClause(m => m.categoryId == categoryId&&m.thridCategoryId== thirdCategoryId);
            jm.data = list;
            jm.status = true;
            jm.code = 0;
            jm.msg = "数据调用成功!";
            return jm;
        }

        #endregion
    }
}
